package org.javaboy.meeting.controller;

import org.javaboy.meeting.model.Employee;
import org.javaboy.meeting.model.Pager;
import org.javaboy.meeting.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class EmployeeController {

    public static final Integer PAGE_SIZE = 10;

    @Autowired
    EmployeeService employeeService;

    /**
     * 开启或者关闭员工账号
     * @param id 用户id
     * @param status 代表用户状态
     *               状态2为关闭 1为开启 0为待审批
     *
     * @return
     */
    @RequestMapping("/updateemp")
    public String updateemp(Integer id,Integer status){
        if(status!=2){
            employeeService.updatestatus(id,2);}
        else{
            employeeService.updatestatus(id,1);
        }
        return "redirect:/admin/getEmpsbyPage?status=1&start=0";
    }

    /**
     *初始状态为1的员工的分页使用
     * @param model
     * @param start 当前页面开始项
     * @param status 用户状态
     * @return
     */
    @RequestMapping("/getEmpsbyPage")
    public String getEmpsbyPage(Model model,int start,int status){
        Pager<Employee> employeePager = employeeService.getEmpsbyPage(start,10,status);
        model.addAttribute("status",status);
        model.addAttribute("emps",employeePager);
        model.addAttribute("pagenumber", Math.ceil(employeePager.getPageStart()/10)+1);
        return "allemployees";
    }

    /**
     * 搜索员工分页使用
     * @param model
     * @param start 当前页的开始项
     * @param status 搜索员工的状态
     * @param employeename 搜索员工的姓名
     * @param username 搜索员工的用户名
     * @return
     */
    @RequestMapping("/searchEmpsbyPage")
    public String searchEmpsbyPage(Model model,int start,int status,String employeename,String username){
        Pager<Employee> employeePager = employeeService.searchEmpsbyPage(start,10,employeename,username,status);
        model.addAttribute("status",status);
        model.addAttribute("emps",employeePager);
        model.addAttribute("employeename",employeename);
        model.addAttribute("username",username);
        model.addAttribute("pagenumber", Math.ceil(employeePager.getPageStart()/10)+1);
        return "searchemployees";
    }
}
